﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Wpf2.Models;

namespace Wpf2.Views
{
    /// <summary>
    /// Interaction logic for formEditBeforePrintAddOrder.xaml
    /// </summary>
    public partial class FormEditBeforePrintAddOrder
    {
        public ObservableCollection<Products> ListProducts { get; set; }
        public decimal TotalPayment;
        bool _isMarcle;

        public FormEditBeforePrintAddOrder(IEnumerable<Products> listPro, decimal totalPayment, bool isMarcle)
        {
            try
            {
                InitializeComponent();
                _isMarcle = isMarcle;
                TotalPayment = totalPayment;
                txtTotalPayment.Text = TotalPayment + " VNĐ";
                ListProducts = new ObservableCollection<Products>();
                foreach (var newPro in listPro.Select(pro => new Products
                                                                 {
                                                                          ProductIdAuto = pro.ProductIdAuto,
                                                                          ProductName = pro.ProductName,
                                                                          ProductId = pro.ProductId,
                                                                          ProductDescription = pro.ProductDescription,
                                                                          InventoryCount = pro.InventoryCount,
                                                                          CostPrice = pro.CostPrice,
                                                                          SellPrice = pro.SellPrice,
                                                                          //thêm mới 11/5 => Đá Hoa Cương
                                                                          ProductHeight = pro.ProductHeight,
                                                                          ProductWidth = pro.ProductWidth,
                                                                          ProductM2 = pro.ProductM2,
                                                                          IsMarble = isMarcle,
                                                                          //
                                                                          Unit = pro.Unit,
                                                                          catalog = pro.catalog,
                                                                          TotalOutPayment = pro.TotalOutPayment,
                                                                          TotalInPayment = pro.TotalInPayment
                                                                      }))
                {
                    ListProducts.Add(newPro);
                }
                DataContext = ListProducts;
                if (!isMarcle)
                    hideColumnOfMarcle();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }


        /// <summary>
        /// Ẩn 3 cột liên quan đế Đá Hoa Cương
        /// </summary>
        private void hideColumnOfMarcle()
        {
            dtgInventory.Columns[3].Visibility = System.Windows.Visibility.Hidden;
            dtgInventory.Columns[4].Visibility = System.Windows.Visibility.Hidden;
            dtgInventory.Columns[6].Visibility = System.Windows.Visibility.Hidden;
        }
        private void BtnPrintClick(object sender, RoutedEventArgs e)
        {
            try
            {
                IDictionary<string, string> param = new Dictionary<string, string>();
                param.Add("StoreName", txtStoreName.Text);
                param.Add("Address", txtAddress.Text);
                param.Add("Mobile", txtMobile.Text);
                param.Add("Phone", txtPhone.Text);
                param.Add("TotalPayment", TotalPayment + "");

                var cryRep = new FormCrystalReportAddInventory(ListProducts, DateTime.Now, param, _isMarcle);
                cryRep.ShowDialog();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
            
        }
        //????ai fix code???
        private void ExcuteAddNewRow(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Enter || e.Key == Key.Tab)
                    ListProducts.Add(new Products());
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

        private void IsAcceptNumberic(object sender, TextCompositionEventArgs e)
        {
            try
            {
                if (!char.IsDigit(e.Text, e.Text.Length - 1))
                {
                    e.Handled = true;
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

        private void TextchangedCaculationPayment(object sender, TextChangedEventArgs e)
        {
            try
            {
                CaculationPaymentForAllProduct();
            }
            catch (Exception ex)
            {
                
               MessageBox.Show(ex.Message);
            }
        }

        private void CaculationPaymentForAllProduct()
        {
            try
            {
                TotalPayment = 0;
                foreach (Products pro in ListProducts)
                    //if (!string.IsNullOrEmpty(pro.ProductId))
                    TotalPayment += pro.TotalInPayment;
                txtTotalPayment.Text = TotalPayment + " VNĐ";
            }
            catch (Exception ex)
            {
                
                MessageBox.Show(ex.Message);
            }

        }

    }
}
